<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ko">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>게시판 수정</title>
	<script src="../js/jquery.js" type="text/javascript"></script>
	<script src="../js/validator.js" type="text/javascript"></script>
	<link rel="stylesheet" type="text/css" href="../lib/editor/stylesheets/xq_ui.css" />
	<script type="text/javascript" src="../lib/editor/javascripts/XQuared.js?load_others=1"></script>
	<script type="text/javascript" src="../lib/editor/javascripts/plugin/FileUploadPlugin.js"></script>
	<script type="text/javascript">
	var xed; 
	
	window.onload = function() {
		xed = new xq.Editor("xqEditor");
	
		xed.isSingleFileUpload = true;
		xed.addPlugin('FileUpload');
		xed.setFileUploadTarget('/common/imageupload.do', null);
		xed.setEditMode('wysiwyg');
		xed.setWidth("100%");
	};
	</script>
	<script type="text/javascript">
        function reg(frm)
        {
			if(!checkAuth()) return false;
        	if (!isTextValidate(frm.article_title, "string", 0, "제목"))			return false;
    		if (!isTextValidate(frm.contents, "string", 0, "내용"))			return false;
    		
    		//내용
    		$('[name=article_content]').val(frm.contents.value);
    		
    		//상단공지여부
    		if ($('input[name="article_notice"]').is(":checked"))
			{
    			$('[name=article_notice_yn]').val('Y');
			}
			else
			{
				$('[name=article_notice_yn]').val('N');	
			}

    		
    		//삭제파일
    		var delFileName = new Array;
			delFileName[0] = new Array();
			var i = 0;
			$('input:checkbox[id="fileCheck"]').each(function(){
    			alert("test");
				if($(this).is(":checked")){
					alert($(this).val());
					delFileName[i] = $(this).val();
					i += 1;
				}				
			});
			$("input[name=delFileList]").val(delFileName);
			
    		return true;
        }
        //에디트
        function sendit()
        {
        	var frm = document.forms["frm"];
        	var contents = xed.getCurrentContent(true);
        	if (contents == "<p>&nbsp;</p>")
        	{
        		alert("내용을 입력하세요.");
        		return false;
        	}

        	return true;
        }
       
		$(function() {
			//첨부파일 추가		
			$('input[name=fileAdd]').click(function(){
				
				var count = 0;	
				
				var fileNum = 1;
				$('input:file[id="file"]').each(function(){
					count = count + 1;
					fileNum = fileNum + 1;
				});
				$('input:[id="fileCheck"]').each(function(){
						count = count + 1;	
				});
				
				var bbs_file_count = $('[name=bbs_file_count]').val();
				var html = "<input type='file' id='file' name='file"+fileNum+"' /><br />";
			
				if(bbs_file_count <= count ){
					alert("첨부파일은 "+bbs_file_count+"개 이상 첨부할 수 없습니다.");
					return false;
				}
				
				$('#fileTD').append(html);
				
			});
			
		});	
    </script>
</head>

<body>
		<!-- 본문 영역 -->
		<div id="contents">
			<!-- 제목 및 Location 영역 -->
			<div id="location">
				<h3 id="contentTitle"></h3>
				<div id="locationPath"></div>
			</div>
			<script type="text/javascript">
			// 현재 메뉴 경로를 설정한다.
			var pagePath = licationPth.split("_");
			var pathStr = "홈";
			for( var i=0; i < pagePath.length; i++ )
			{
				if (i+1 >= pagePath.length)
					pathStr += " &gt; <strong>" + pagePath[i] + "</strong>";
				else
					pathStr += " &gt; " + pagePath[i]
			}
			document.getElementById("contentTitle").innerHTML = pagePath[ pagePath.length - 1 ];
			document.getElementById("locationPath").innerHTML = pathStr;
			</script>
			<!-- 본문 내용 -->
			<div id="content">
				<form name="writeFrm" method="post" action="../bbs/${ bbsMap.BBS_ID }_proc.do" onsubmit="return reg(this);" enctype="multipart/form-data">
				<input type="hidden" name="mode" value="edit" />
				<input type="hidden" name="bbs_id" id="bbs_id" value="${ bbsMap.BBS_ID }" />
				<input type="hidden" name="bbs_file_count" id="bbs_file_count" value="${ bbsMap.BBS_FILE_COUNT}"/>
				<input type="hidden" name="file_count" id="file_count" value="${ viewMap.FILE_COUNT}"/>
				<input type="hidden" name="file_relate_id" id="file_relate_id" value="${ viewMap.FILE_RELATE_ID}"/>
				<input type="hidden" name="article_id" id="article_id" value="${ viewMap.ARTICLE_ID }" />
				<input type="hidden" name="article_editor" id="article_editor" value="${ adminInfo.admin_name }" />
				<input type="hidden" name="article_editor_id" id="article_editor_id" value="${ adminInfo.admin_id }" />
				<input type="hidden" name="article_content" id="article_content" value="" />
				<input type="hidden" name="article_reg_sort" id="article_reg_sort" value="${ viewMap.ARTICLE_REG_SORT}" />
    			<div class="ar">
					<span class="button btnProc01"><input type="submit" value="저장" /></span>
					<span class="button btnProc01"><a href="#" onclick="history.go(-1); return false;">취소</a></span>
				</div>
				<table cellspacing="0" cellpadding="0" border="0" class="tbl_type04">
					<colgroup>
						<col width="150px" />
						<col width="300px" />
						<col width="150px" />
						<col />
					</colgroup>
					<tbody>
						<tr>
							<th>제목</th>
							<td colspan="3"><input type="text" name="article_title" size="105" maxlength="200" value="${ viewMap.ARTICLE_TITLE }" /></td>
						</tr>
						<%-- <tr>
							<th>상단공지</th>
							<td colspan="3">
								<input type="checkbox" name="article_notice" <c:if test="${ viewMap.ARTICLE_NOTICE_YN eq 'Y' }" >checked </c:if> />
								<input type="hidden" name="article_notice_yn" value="" />
							</td>
						</tr> --%>
						<c:if test="${ bbsMap.BBS_CATEGORY_YN eq 'Y' }" >
						<tr>
							<th>카테고리</th>
							<td colspan="3">
								<select name="article_category">
								<c:set var="category" value="${fn:split(bbsMap.BBS_CATEGORY,'|')}" />
								<c:forEach var="items" items="${category}" varStatus="g">
									<option value="${items}" <c:if test="${ items eq viewMap.ARTICLE_CATEGORY }">selected="selected"</c:if>>${items}</option>
								</c:forEach> 
								</select>
							</td>
						</tr>
						</c:if>
						<%-- <tr>
							<th>공개여부</th>
							<td colspan="3">
								<input type="radio" name="article_open_yn" value="Y" <c:if test="${ viewMap.ARTICLE_OPEN_YN eq 'Y' }">checked</c:if> /> 공개
								<input type="radio" name="article_open_yn" value="N" <c:if test="${ viewMap.ARTICLE_OPEN_YN eq 'N' }">checked</c:if>/> 비공개
							</td>
						</tr> --%>
						<tr>
							<th>등록자</th>
							<td colspan="3"><input type="text" name="article_writer" size="30" maxlength="50" value="${ viewMap.ARTICLE_WRITER }" /></td>
						</tr>
						<tr>
							<td colspan="4"><textarea id="xqEditor" name="contents">${ viewMap.ARTICLE_CONTENT }</textarea></td>
						</tr>
						<c:if test="${ bbsMap.BBS_FILE_YN eq 'Y'}">	<!-- 게시판이 첨부파일기능을 사용할 경우 -->
						<tr>
							<th>첨부파일</th>
							<td colspan="3">
								<div id="fileTD">
								<c:if test="${ viewMap.FILE_COUNT < bbsMap.BBS_FILE_COUNT }">
								<input type="file" id="file" name="file1" />&nbsp;<span class="button btnProc01"><input type="button" name="fileAdd" value="추가" /></span><br />
								</c:if>								
								</div>
								<c:forEach items="${ fileListMap }" var="fileListMap">
								<c:out value="${ fileListMap.FILE_NAME }" /><input type="hidden" id="fileCheck" /><br />
								</c:forEach>
							</td>
						</tr>
						</c:if>
					</tbody>
				</table>
				</form>
				<form name="fileFrm" method="get" action="../bbs/${ bbsMap.BBS_ID }_file_proc.do" onsubmit="return reg(this);" >
				</form>
			</div>
		</div>
</body>
</html>
